package managedbean.stakeholder;

import entity.AdvertisingEntity;

import exception.ExistException;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpSession;
import session.stateless.AdvertisementSessionBean;

/**
 *
 * @author KohLi
 */
@ManagedBean
@ViewScoped
public class ViewAdvertisementManagedBean implements Serializable {

    private AdvertisingEntity advertisement;
    private Long stakeholderId;
    private List<AdvertisingEntity> adsList;
    @EJB
    private AdvertisementSessionBean AdvertisementSessionBean;

    public void initView(PhaseEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);

        //Clear previous set messages(delete)
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();

        stakeholderId = (Long) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderId");
        if (stakeholderId == null) {

            //Session not save. (redirect Stakeholder to login.
            String nextPage = FacesContext.getCurrentInstance().getExternalContext().encodeActionURL(FacesContext.getCurrentInstance().getApplication().
                    getViewHandler().getActionURL(FacesContext.getCurrentInstance(), "/StakeholderLogin.xhtml"));
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect(nextPage);
            } catch (IOException ioe) {
                ioe.printStackTrace();

            }
        } else {
            //load the datatable

            getAdvertisements();


        }
    }

    public List<AdvertisingEntity> getAdvertisements() {
        //get the stakeholder id from session
        //stakeholderId = (Long) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderId");
        adsList = new ArrayList();

        setAdsList(AdvertisementSessionBean.getAllAdvertisementByStakeholder(stakeholderId));
        if (getAdsList().isEmpty()) {
            System.out.println("no available branches");
        }
        return getAdsList();
    }

    public void redirectPage(ActionEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {


        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("stakeholderId", stakeholderId);
        FacesContext ctx = FacesContext.getCurrentInstance();
        String nextPage = ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().
                getActionURL(ctx, "/stakeholder/AddAdvertisement.xhtml"));
        try {
            ctx.getExternalContext().redirect(nextPage);
        } catch (IOException ex) {
            //file dont exist.
        }

    }

    public void editAds(ActionEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("stakeholderId", stakeholderId);
        FacesContext ctx = FacesContext.getCurrentInstance();
        String nextPage = ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().
                getActionURL(ctx, "/stakeholder/EditAdvertisement.xhtml"));
        try {
            ctx.getExternalContext().redirect(nextPage);
        } catch (IOException ex) {
            //file dont exist.
        }
    }

    public void view_Edit_Branch(AdvertisingEntity ads) throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedAd", AdvertisementSessionBean.getAdsById(ads.getId()));

        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("stakeholderId", stakeholderId);
        FacesContext ctx = FacesContext.getCurrentInstance();
        String nextPage = ctx.getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().
                getActionURL(ctx, "/stakeholder/EditAdvertisement.xhtml"));
        try {
            ctx.getExternalContext().redirect(nextPage);
        } catch (IOException ex) {
            //file dont exist.
        }

    }

    public void view_Delete_Branch(AdvertisingEntity ads) throws IOException {
        System.out.println("advertisement id:" + ads.getId().toString());
        boolean success;
        FacesMessage msg = new FacesMessage();

        try {
            success = AdvertisementSessionBean.deleteAds(ads.getId());
            if (success = true) {
                msg = new FacesMessage("Advertisement deleted successfully.");

            }
        } catch (Exception ex) {
            msg = new FacesMessage(ex.getMessage());

        }
        FacesContext.getCurrentInstance().addMessage(null, msg);


    }

    /**
     * @return the advertisement
     */
    public AdvertisingEntity getAdvertisement() {
        return advertisement;
    }

    /**
     * @param advertisement the advertisement to set
     */
    public void setAdvertisement(AdvertisingEntity advertisement) {
        this.advertisement = advertisement;
    }

    /**
     * @return the stakeholderId
     */
    public Long getStakeholderId() {
        return stakeholderId;
    }

    /**
     * @param stakeholderId the stakeholderId to set
     */
    public void setStakeholderId(Long stakeholderId) {
        this.stakeholderId = stakeholderId;
    }

    /**
     * @return the adsList
     */
    public List<AdvertisingEntity> getAdsList() {
        return adsList;
    }

    /**
     * @param adsList the adsList to set
     */
    public void setAdsList(List<AdvertisingEntity> adsList) {
        this.adsList = adsList;
    }
}
